<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="${basePackageName}.mapper.${subPackAndclassName}Mapper">
    <sql id="where">
        <where>
            <trim>

            </trim>
        </where>
    </sql>
    <!-- 新增-->
    <insert id="insert" parameterType="${basePackageName}.po.${subPackAndclassName}" useGeneratedKeys="true" keyProperty="${pKey[1]}">
        insert into ${tableName}
        <trim prefix="(" suffix=")" suffixOverrides=",">
        <#list fieldList as var>
            <if test="${var[0]} != null">${var[2]},</if>
        </#list>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
        <#list fieldList as var>
            <if test="${var[0]} != null ">${r"#{"}${var[0]}${r"}"},</if>
        </#list>
        </trim>
    </insert>

    <!-- 批量添加-->
    <insert id="batchInsert" parameterType="java.util.List">
        insert into ${tableName}
        <trim prefix="(" suffix=")" suffixOverrides=",">
        <#list fieldList as var> ${var[2]}, </#list>
        </trim>
        values
        <foreach collection="list" item="item" index="index" separator=",">
            <trim prefix=" (" suffix=")" suffixOverrides=",">
            <#list fieldList as var>
            ${r"#{item."}${var[0]}${r"}"},
            </#list>
            </trim>
        </foreach>
    </insert>


    <!-- 删除-->
    <delete id="deleteById">
        delete t.* from ${tableName} t where t.${pKey[0]} = ${r"#{"}${pKey[1]}${r"}"}
    </delete>

    <!-- 修改 -->
    <update id="updateById" parameterType="${basePackageName}.po.${subPackAndclassName}">
        update ${tableName} t
        <trim prefix="set" suffixOverrides=",">
        <#list fieldList as var>
            <if test="${var[0]} != null ">${var[2]} = ${r"#{"}${var[0]}${r"}"},</if>
        </#list>
        </trim>
        where t.${pKey[0]} = ${r"#{"}${pKey[1]}${r"}"}
    </update>

    <!-- 通过id获取数据 -->
    <select id="selectById" resultType="${basePackageName}.po.${subPackAndclassName}">
        select * from ${tableName} t where t.${pKey[0]} = ${r"#{"}${pKey[1]}${r"}"}
    </select>

    <!-- 列表 -->
    <select id="selectList" resultType="${basePackageName}.po.${subPackageName}.${className}">
        select
        <if test="showField != null and showField != ''">${r"${"}showField${r"}"}</if>
        <if test="showField == null or showField == ''">t.*</if>
        from ${tableName} t
        <include refid="where" />
        <if test="orderField != null and orderField !='' ">
            order by ${r"${"}orderField${r"}"}
            <if test="orderType != null and orderType !='' ">
            ${r"${"}orderType${r"}"}
            </if>
        </if>
    </select>

    <!-- 列表 -->
    <select id="selectMaps" resultType="HashMap">
        select
        <if test="showField != null and showField != ''">${r"${"}showField${r"}"}</if>
        <if test="showField == null or showField == ''">t.*</if>
        from ${tableName} t
        <include refid="where" />
        <if test="orderField != null and orderField !='' ">
            order by ${r"${"}orderField${r"}"}
            <if test="orderType != null and orderType !='' ">
            ${r"${"}orderType${r"}"}
            </if>
        </if>
    </select>

    <!-- 分页显示列表 -->
    <select id="selectPage" resultType="${basePackageName}.po.${subPackageName}.${className}">
        select
        <if test="showField != null and showField != ''">${r"${"}showField${r"}"}</if>
        <if test="showField == null or showField == ''">t.*</if>
        from ${tableName} t
        <include refid="where" />
        <if test="orderField != null and orderField !='' ">
            order by ${r"${"}orderField${r"}"}
            <if test="orderType != null and orderType !='' ">
            ${r"${"}orderType${r"}"}
            </if>
        </if>
        <if test="start != null and showCount != null">
            limit ${r"#{"}start${r"}"}, ${r"#{"}showCount${r"}"}
        </if>
    </select>

    <!-- 分页显示列表 -->
    <select id="selectMapsPage" resultType="HashMap">
        select
        <if test="showField != null and showField != ''">${r"${"}showField${r"}"}</if>
        <if test="showField == null or showField == ''">t.*</if>
        from ${tableName} t
        <include refid="where" />
        <if test="orderField != null and orderField !='' ">
            order by ${r"${"}orderField${r"}"}
            <if test="orderType != null and orderType !='' ">
            ${r"${"}orderType${r"}"}
            </if>
        </if>
        <if test="start != null and showCount != null">
            limit ${r"#{"}start${r"}"}, ${r"#{"}showCount${r"}"}
        </if>
    </select>

    <select id="selectCount" parameterType="HashMap" resultType="int">
        select count(1) from ${tableName} t
        <include refid="where" />
    </select>
</mapper>